#!/bin/bash

# Slackware build script for mitmproxy-bin

# Written by B. Watson (yalhcru@gmail.com)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# I was gonna package this up from source, but it just has too many
# dependencies, many of which aren't on SBo already. I'd have to add
# at least 15 or 20 python modules, and I just won't. The official
# binary release is "frozen" executables with no external Python
# dependencies (just shared libs that are part of Slackware already).

# This is mostly just a binary repack of the official build. Which
# is only for x86_64 Linux (sorry, 32-bit users).

# I named this with the -bin suffix so that someday someone (other
# than me!) can do a proper build-from-source mitmproxy SlackBuild,
# with tons of dependencies in REQUIRES...

# There's *nothing* in the Linux tarball besides the executables, so
# I've also included some docs from the source tarball, but not the
# complete API docs (again, to avoid a long dependency chain of the
# stuff needed to build them). Use https://docs.mitmproxy.org/stable/
# for that.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=mitmproxy-bin
SRCNAM=mitmproxy
VERSION=${VERSION:-7.0.4}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

# Binary repack, only supports:
ARCH=x86_64

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# If the user's trying to build this on a non-x86_64 box, it's
# almost certainly a mistake on the user's part... but maybe not,
# for smart users. So warn and pause, but allow it to happen.
M="$(uname -m)"
if [ "$M" != "x86_64" ]; then
  W="* WARNING: cross-building x86_64 package on $M host. *"
  H="$( echo "$W" | sed 's,.,*,g' )"
  echo -e "$H\\n$W\\n$H\\n"
  cat <<EOF
You're about to build a 64-bit (x86_64) package, which will work fine
if you install it on an x86_64 Slackware64 system... but not on *this*
system, because it's $M, not x86_64.

Press Enter or wait 5 seconds to continue. Or, press ^C to abort.
EOF
  read -p "> " -t 5
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$VERSION
mkdir -p $SRCNAM-$VERSION
cd $SRCNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION-linux.tar.gz
tar xvf $CWD/$SRCNAM-$VERSION.tar.gz
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# Already stripped.
mkdir -p $PKG/usr/bin
install -m0755 $SRCNAM mitmdump mitmweb $PKG/usr/bin

# Stub man page. Just stuff copied from the README and a pointer to the
# actual docs on the web.
mkdir -p $PKG/usr/man/man1
gzip -9c < $CWD/$SRCNAM.1 > $PKG/usr/man/man1/$SRCNAM.1.gz
ln -s $SRCNAM.1.gz $PKG/usr/man/man1/mitmdump.1.gz
ln -s $SRCNAM.1.gz $PKG/usr/man/man1/mitmweb.1.gz

# Include README, etc from source tarball.
cd $SRCNAM-$VERSION
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a *.md LICENSE $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
